require "Common/define"
require "Logic/Config"
require "Common/functions"


---@class NewPlayerActiveManager
NewPlayerActiveManager = NewPlayerActiveManager or {}
local this = NewPlayerActiveManager

local playerData = nil
local boxData = nil
local taskData = nil
local extraBoxData = nil
local extraTaskIdLst = nil

function NewPlayerActiveManager.init()
    playerData = {}
    boxData = {}
    taskData = {}
    extraBoxData ={}
    extraTaskIdLst = {}
    this.InitBoxData()
    this.InitTaskData()
    this.InitExtraBoxData()
end

function NewPlayerActiveManager.InitBoxData()
    local boxCfg = G.dataTable["t_newplayerbox"]
    if boxCfg == nil then return end
    local i = 1
    for _,v in pairs(boxCfg) do
        --v.state = 0
        table.insert(boxData, v)
        --boxData[i] = v
        boxData[i].state = 0 --0，未达成  1，可领取  2，已领取
        i = i+1
    end
    table.sort(boxData,function(a,b) return a.f_BoxID < b.f_BoxID end)
end

function NewPlayerActiveManager.InitTaskData()
    local allTaskcfg = G.dataTable["t_periodtask"]
    if allTaskcfg == nil then return end
    local i = 1
    for _,v in pairs(allTaskcfg) do
        if v.f_ActivityId == 18001 and v.f_ActTaskType == 2201 then
            --v.state = 0
            table.insert(taskData, v)
            taskData[i].state = 0 --0，未开启  1，未完成 2，可领取 3，已领取
            taskData[i].process = 0
            i = i+1
        end
    end
    table.sort(taskData,function(a,b) return a.f_ActTaskId < b.f_ActTaskId end)
end

function NewPlayerActiveManager.InitExtraBoxData()
    local extraBoxCfg = G.dataTable["t_periodtask"]
    if extraBoxCfg == nil then return end
    local i = 1
    for _,v in pairs(extraBoxCfg) do
        if v.f_ActivityId == 18001 and v.f_ActTaskType == 2202 then
            --v.state = 0 --0，未达成  1，可领取  2，已领取
            table.insert(extraBoxData, v)
            table.insert(extraTaskIdLst, v.f_ActTaskId)
            extraBoxData[i].state = 0 --0，未达成  1，可领取  2，已领取
            extraBoxData[i].process = 0
            i = i+1
        end
    end
    table.sort(extraBoxData,function(a,b) return a.f_ActTaskId < b.f_ActTaskId end)
end

function NewPlayerActiveManager.SaveNewPlayerInfo(msg)
    playerData.inviteCode = msg.shareActivityInfo.seflInviteCode
    --playerData.inviteNum = msg.shareActivityInfo.inviteNum
    playerData.score = msg.shareActivityInfo.score
    playerData.useCodeCount = msg.shareActivityInfo.useCodeCount
    if msg.shareActivityInfo.useCodeCount > 0 then
        for k,v in pairs(taskData) do
            v.state = 1
        end
    end
    this.UpdateData(msg)
    FireEvent(Config.EventType.NewPlayerRefresh)
end

function NewPlayerActiveManager.UpdateData(msg)
    --设置宝箱状态
    for m, n in pairs(boxData) do
        if playerData.score >= n.f_NeedExpCount then
            if this.CheckInTable(n.f_BoxID, msg.shareActivityInfo.recvScoreAwardIds) then
                n.state = 2
            else
                n.state = 1
            end
        end
    end

    for k,v in pairs(msg.shareActivityInfo.taskInfo) do
        --设置邀请人数宝箱的状态
        for m,n in pairs(extraBoxData) do
            if v.taskId == n.f_ActTaskId then
                n.process = v.process or 0
                if v.process >= n.f_Progress then
                    if v.recvStatus == 0 then
                        n.state = 1
                    else
                        n.state = 2
                    end
                else
                    n.state = 0
                end
            end
        end

        --设置任务状态
        if msg.shareActivityInfo.useCodeCount > 0 then
            for m,n in pairs(taskData) do
                if v.taskId == n.f_ActTaskId then
                    if v.process >= n.f_Progress then
                        if v.recvStatus == 0 then
                            n.state = 2
                        else
                            n.state = 3
                        end
                    else
                        n.state = 1
                    end
                end
            end
        end
    end
end

function NewPlayerActiveManager.GetFunctionSwitchId()
    return tonumber(G.dataTable["t_global"]["599"].f_string_value)
end

function NewPlayerActiveManager.GetPlayerSelfData()
    return playerData
end

function NewPlayerActiveManager.SetBoxData(boxId, state)
    for k,v in pairs(boxData) do
        if boxId == v.f_BoxID then
            v.state = state
        end
    end
end

function NewPlayerActiveManager.SetExtraBoxData(taskId, state)
    for k,v in pairs(extraBoxData) do
        if taskId == v.f_ActTaskId then
            v.state = state
        end
    end
end

function NewPlayerActiveManager.SetTaskData(taskId, state)
    for k,v in pairs(taskData) do
        if taskId == v.f_ActTaskId then
            v.state = state
        end
    end
end

function NewPlayerActiveManager.GetBoxAndTaskData()
    return boxData,extraBoxData,taskData
end

function NewPlayerActiveManager.CheckInTable(ele, tab)
    for _,v in pairs(tab) do
        if ele == v then
            return true
        end
    end
    return false
end

function NewPlayerActiveManager.GetCurExtraRewardTaskIdx()
    local index = #extraBoxData
    --有可领取，直接返回等级最低的可领取
    for k,v in pairs(extraBoxData) do
        if v.state == 1 then
            return k
        end
    end
    --没有可领取，返回最低的未达成
    for k,v in pairs(extraBoxData) do
        if v.state == 0 then
            return k
        end
    end
    --返回最后一个任务
    return index
end

function NewPlayerActiveManager.GetTypeByTaskId(taskId)
    for k,v in pairs(extraBoxData) do
        if v.f_ActTaskId == taskId then
            return 2202
        end
    end
    for k,v in pairs(taskData) do
        if v.f_ActTaskId == taskId then
            return 2201
        end
    end
    return 0
end

--检查是否有任务奖励待领取
function NewPlayerActiveManager.CheckTaskAwardComplete()
    for k,v in pairs(taskData) do
        if v.state ~= 3 then
            return false
        end
    end
    return true
end

function NewPlayerActiveManager.GetRandomShowSkinId()
    local showSkinIdList = decodeJsonStrToTabel(G.dataTable["t_global"]["605"].f_string_value) or {}
    local count = #showSkinIdList
    local showSkinIdIdx = math.random(1,count)
    return showSkinIdList[showSkinIdIdx]
end

function NewPlayerActiveManager.GetInviteCode()
    return playerData.inviteCode or "123"
end

return NewPlayerActiveManager
